package com.ruoyi.system.service.shopping.impl;

import com.ruoyi.system.domain.shopping.Category;
import com.ruoyi.system.domain.shopping.Product;
import com.ruoyi.system.domain.shopping.dto.ProductVoDto;
import com.ruoyi.system.domain.shopping.vo.ProductVo;
import com.ruoyi.system.mapper.shopping.PictureMapper;
import com.ruoyi.system.mapper.shopping.ProductMapper;
import com.ruoyi.system.service.shopping.CollectService;
import com.ruoyi.system.service.shopping.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private CollectService collectService;
    @Override
    public List<ProductVo> selectProductVoList(Product product) {
        return productMapper.selectProductVoList(product);
    }

    @Override
    public List<Category> selectProductHostList() {
        return productMapper.selectProductHostList();
    }

    @Override
    public ProductVo selectProductVoById(Integer productId) {
        return productMapper.selectProductVoById(productId);
    }

    @Override
    public ProductVoDto selectProductVoDtoByproductId(Integer productId,Integer userId) {
        ProductVo productVo = productMapper.selectProductVo(productId);
        ProductVoDto productVoDto = new ProductVoDto();
        BeanUtils.copyProperties(productVo,productVoDto);
        Boolean existCollect = collectService.isExistCollect(userId, productId);
        productVoDto.setIsCollect(existCollect ? 1 : 0);
        productVoDto.setPictures(pictureMapper.selectPictureByPid(productId));
        return productVoDto;
    }

    @Override
    public List<ProductVo> selectProductList(Product product) {
        return productMapper.selectProductList(product);
    }

    @Override
    public Boolean add(Product product) {
        return productMapper.insert(product)>0;
    }

    @Override
    public Boolean update(Product product) {
        return productMapper.update(product)>0;
    }

    @Override
    public Boolean deleteByIds(Integer[] productId) {
        return productMapper.deleteByIds(productId) > 0;
    }

    @Override
    public Product selectProductById(Integer productId) {
        return productMapper.selectProductById(productId);
    }
}
